package com.haozhen

/**
  * @author haozhen
  * @email haozh@ync1.com
  * @date 2020/12/8  20:18
  *
  *       作业题：
  *       1、百元喝酒
  *       作业要求：每瓶啤酒2元，3个空酒瓶或者5个瓶盖可换1瓶啤酒。100元最多可喝多少瓶啤酒？（不允许借啤酒）
  *       思路：利用递归算法，一次性买完，然后递归算出瓶盖和空瓶能换的啤酒数
  *
  */
class Drink {

  //中共喝了几瓶酒
  var totalWine:Int=_;
  //当前几瓶酒
  var wine:Int=_
  //酒瓶
  var bottle:Int=_;

  //瓶盖
  var cap:Int=_;

  def swapWine(): Unit ={
    //每瓶酒 包括一个酒瓶和一个瓶盖
    println(s"开始换酒 当前有酒$wine 瓶，有瓶盖 $cap 个 ，有空酒瓶 $bottle 瓶")

    bottle = bottle + wine;
    cap = cap + wine
    totalWine += wine
    wine=0;

    if(bottle<3 && cap<5){
      println(s"换酒结束 当前有酒$wine 瓶，有瓶盖 $cap 个 ，有空酒瓶 $bottle 瓶, 中共喝了 $totalWine 瓶酒")
    }else{
      if(bottle>=3){
        val (currentSwapWine,currentBottle) = bottleSwap()
        wine += currentSwapWine
        bottle = currentBottle
      }
      if(cap>=5){
        val  (currentSwapWine,currentCap) =  capSwap()
        wine += currentSwapWine
        cap = currentCap
      }
      println(s"该轮换酒结束换酒 当前有酒$wine 瓶，有瓶盖 $cap 个 ，有空酒瓶 $bottle 瓶")
      swapWine()
    }

  }

  def bottleSwap()={

    (bottle/3,bottle%3)
  }

  def capSwap()={
    (cap/5,cap%5)
  }

}

object Drink{
  def main(args: Array[String]): Unit = {
    val drink  = new Drink
    //当前有金额 100元 每瓶酒2元
    drink.wine = 100/2
    drink.swapWine()
  }
}
